﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using DAL;

namespace Business
{
    public static class CategoriesManager
    {
        // CRUD

        #region Create (Add)

        public static void Create(Category category)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                context.Category.AddObject(category);
                context.SaveChanges();
            }
        }

        #endregion

        #region Read (Get)

        public static List<Category> Read()
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                return context.Category.ToList();
            }
        }

        public static Category Read(int id)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                try
                {
                    return context.Category.Include("Article").Single(x => x.ID == id);
                }
                catch (Exception e)
                { return null; }
            }
        }

        #endregion

        #region Update (Set)

        public static void Update(Category category)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                Category c = new Category();
                c = context.Category.Single(x => x.ID == category.ID);
                c.Name = category.Name;
                c.Description = category.Description;

                context.SaveChanges();
            }
        }

        #endregion

        #region Delete (Delete)

        public static void Delete(Category category)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                context.Category.DeleteObject(category);
                context.SaveChanges();
            }
        }

        public static void Delete(int ID)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                Category c = context.Category.Where(x => x.ID == ID).First();
                context.Category.DeleteObject(c);
                context.SaveChanges();
            }
        }

        #endregion

    }
}
